import { __private, _decorator, AudioClip, Component, input, Input, instantiate, math, Node, Prefab } from 'cc';
import { GameManager } from './GameManager';
import { Enemy } from './Enemy';
import { AudioMgr } from './AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('EnemyManager')
export class EnemyManager extends Component {

    private static instance:EnemyManager = null;
    static getInstance() {
        return this.instance;
    }

    @property
    enemy0SpawnRate: number = 1;
    @property(Prefab)
    enemy0Prefab: Prefab = null;
    @property
    enemy1SpawnRate: number = 3;
    @property(Prefab)
    enemy1Prefab: Prefab = null;
    @property
    enemy2SpawnRate: number = 5;
    @property(Prefab)
    enemy2Prefab: Prefab = null;
    @property
    reward1SpawnRate: number = 15;
    @property(Prefab)
    reward1Prefab: Prefab = null;
    @property
    reward2SpawnRate: number = 15;
    @property(Prefab)
    reward2Prefab: Prefab = null;
    // 时间间隔
    doubleClickInterVal: number = 0.3;
    lastClickTime: number = 0;
    // 生成的敌机数组
    @property([Node])
    enemyArray: Node[] = [];
    @property(AudioClip)
    useBomAudio: AudioClip = null;

    protected onLoad(): void {
        input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);

    }

    start() {
        // 单例
        EnemyManager.instance = this;
        // 生成敌机
        this.schedule(this.enemy0Spawn, this.enemy0SpawnRate);
        this.schedule(this.enemy1Spawn, this.enemy1SpawnRate);
        this.schedule(this.enemy2Spawn, this.enemy2SpawnRate);
        // 生成物品奖励
        this.schedule(this.reward1Spawn, this.reward1SpawnRate);
    }

    update(deltaTime: number) {

    }
    protected onDestroy(): void {
        // 取消定时器
        this.unschedule(this.enemy0Spawn);
        this.unschedule(this.enemy1Spawn);
        this.unschedule(this.enemy2Spawn);
        this.unschedule(this.reward1Spawn);
        input.off(Input.EventType.TOUCH_END, this.onTouchEnd, this);

    }
    // 生成敌机
    enemy0Spawn() {
        const enemyNode = this.objectSpawn(this.enemy0Prefab, -215, 215, 450);
        this.enemyArray.push(enemyNode);
    }
    // 生成敌机
    enemy1Spawn() {
        const enemyNode = this.objectSpawn(this.enemy1Prefab, -200, 200, 475);
        this.enemyArray.push(enemyNode);
    }
    // 生成敌机
    enemy2Spawn() {
        const enemyNode = this.objectSpawn(this.enemy2Prefab, -155, 155, 560);
        this.enemyArray.push(enemyNode);
    }
    // 随机数生成
    getRandomInt(min: number, max: number) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    objectSpawn(enemyPrefab: Prefab, minX: number, maxX: number, Y: number): Node {
        const enemy = instantiate(enemyPrefab);
        this.node.addChild(enemy);
        const randomX = math.randomRangeInt(minX, maxX);
        enemy.setPosition(randomX, Y, 0);
        return enemy;
    }
    // 生成奖励
    reward1Spawn() {
        let prefab = null;
        const randomNumber = math.randomRangeInt(0, 2);
        if (randomNumber == 0) {
            prefab = this.reward1Prefab;
        } else if (randomNumber == 1) {
            prefab = this.reward2Prefab;
        }
        this.objectSpawn(prefab, -207, 207, 474);
    }

    onTouchEnd() {
        let currentTime = Date.now();
        let timeDiff = (currentTime - this.lastClickTime) / 1000;// 转换为秒
        if (timeDiff < this.doubleClickInterVal) {
            this.onDoubleClick();
        }
        this.lastClickTime = currentTime;
    }
    onDoubleClick() {
        console.log("双击");
        if (GameManager.getInstance().isHaveBomb() === false) {
            return;
        }
        // 使用炸弹
        GameManager.getInstance().useBomb();
        AudioMgr.inst.playOneShot(this.useBomAudio,0.8);
        for (const e of this.enemyArray) {
            const enemy = e.getComponent(Enemy);
            enemy.killNow( );
        }
    }

    removeEnemy(enemy:Node){
        const index = this.enemyArray.indexOf(enemy);
        if (index != -1) {
            this.enemyArray.splice(index,1);
        }
    }

}

